package com.app.campusguide;

import com.app.sqlite_connect.HandlerSqlite;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;

public class Reset_schedule extends Activity {
	private final String APP_LOG = "Reset_schedule";
	private HandlerSqlite database;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		database = new HandlerSqlite(this);
		setContentView(R.layout.activity_reset_schedule);
		
		Button btn_reset_schedule_cancel =(Button) findViewById(R.id.btn_reset_schedule_cancel);
		Button btn_reset_schedule_acept =(Button) findViewById(R.id.btn_reset_schedule_acept);
		
		btn_reset_schedule_cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	Intent i = new Intent(getApplicationContext(),AcivityMain.class);
        		startActivity(i);
            }      
        });
		btn_reset_schedule_acept.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	Log.d(APP_LOG,"Entro al ONCLICK");
            	Dialog dialog_message = confirmationDialog();
            	dialog_message.show();
            }      
        });

	}
	
	/**
	 * This method manage a dialog to confirm reset action
	 * @return An object Dialog for show on activity
	 */
	private Dialog confirmationDialog()
	{
		Log.d(APP_LOG,"Va a crear el dialogo");
	    AlertDialog.Builder builder = new AlertDialog.Builder(this);
	 
	    builder.setTitle(R.string.str_label_confirm);
	    builder.setMessage(R.string.str_message_confirm_reset);
	    builder.setPositiveButton(R.string.str_uppercase_acept, new OnClickListener() {
	    public void onClick(DialogInterface dialog, int which) {
	    	long l_result_delete = database.deleteAllSchedule();
	    	if(l_result_delete == 0){
	    		TextView text1 = new TextView(getApplicationContext());
		    	text1.setText(R.string.str_successful_process);
		    	showToast (text1.getText().toString());
		    	text1 = null;
	    		Intent i = new Intent(getApplicationContext(),AcivityMain.class);
        		startActivity(i);
	    	}else{
	    		TextView text1 = new TextView(getApplicationContext());
		    	text1.setText(R.string.str_unexpected_error);
		    	showToast (text1.getText().toString());
		    	text1 = null;
	    	}
	        dialog.cancel();
	    }
	    });
	    builder.setNegativeButton(R.string.str_uppercase_cancel, new OnClickListener() {
	    public void onClick(DialogInterface dialog, int which) {
	    	TextView text1 = new TextView(getApplicationContext());
	    	text1.setText(R.string.str_cancel_process);
	    	showToast (text1.getText().toString());
	    	text1 = null;
	    	dialog.cancel();
	    }
	    });
	 
	    return builder.create();
	}
	
	/**
	 * Allow to show a message in a toast of Android OS
	 * @param str_message String with message to show
	 */
	private void showToast(String str_message){
		Toast toast = Toast.makeText(getApplicationContext(), str_message, Toast.LENGTH_LONG);
		View textView = toast.getView();
		LinearLayout lay = new LinearLayout(getApplicationContext());
		lay.setOrientation(LinearLayout.HORIZONTAL);
		ImageView view = new ImageView(getApplicationContext());
		view.setImageResource(android.R.drawable.ic_menu_info_details);
		lay.addView(view);
		lay.addView(textView);
		toast.setView(lay);
		toast.show();
	}
}
